package com.example.jl.bethinner.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.example.jl.bethinner.R;
import com.example.jl.bethinner.base.BaseActivity;
import com.example.jl.bethinner.base.BtApplication;
import com.example.jl.bethinner.service.ServiceUpdateUI;
import com.example.jl.bethinner.util.StatusBarCompat;

public class ServiceTestActivity extends BaseActivity {

    private TextView textView;
    private TextView cancel;
    private TextView start;
    public static final String ACTION_UPDATEUI = "action.updateUI";
    UpdateUIBroadcastReceiver broadcastReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_service_test);
        initView();
        StatusBarCompat.compat(this);
        handler.postDelayed(runnable, 500);
        // 动态注册广播
        IntentFilter filter = new IntentFilter();
        filter.addAction(ACTION_UPDATEUI);
        broadcastReceiver = new UpdateUIBroadcastReceiver();
        registerReceiver(broadcastReceiver, filter);

        // 启动服务
        startService(new Intent(ServiceUpdateUI.ACTION));

        initListener();
    }

    private void initListener() {
        // 结束服务
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                stopService(new Intent(ServiceUpdateUI.ACTION));
            }
        });
        start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startService(new Intent(ServiceUpdateUI.ACTION));
            }
        });
    }

    private void initView() {
        textView = (TextView) findViewById(R.id.count_txt);
        cancel = (TextView) findViewById(R.id.cancel);
        start = (TextView) findViewById(R.id.start);
    }


    Handler handler = new Handler();
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            if (BtApplication.showToast) {
                Toast.makeText(getApplicationContext(), "更新ui", Toast.LENGTH_SHORT).show();
            }
            handler.postDelayed(this, 500);
        }
    };

    /**
     * 定义广播接收器（内部类）
     *
     * @author lenovo
     */
    public class UpdateUIBroadcastReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            int count = intent.getExtras().getInt("count");
            textView.setText(String.valueOf(count));
            if (count % 10 == 0) {
                BtApplication.showToast = true;
            } else {
                BtApplication.showToast = false;
            }
        }

    }

    @Override
    protected void onDestroy() {
        System.out.println("onDestroy");
        super.onDestroy();
        // 注销广播
        unregisterReceiver(broadcastReceiver);
    }
}
